﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Viewer.BusinessObject.BusinessEntities;
using Ext.Net;

namespace Viewer.NhanSu.UserControl
{
    public partial class UCLiLuanChinhTri : System.Web.UI.UserControl
    {
        NSLiLuanChinhTri liLuan = new NSLiLuanChinhTri();
        private List<NSLiLuanChinhTri> lst;
        NSNhanVien nhanvien = new NSNhanVien();
        CommonCode common = new CommonCode();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// Reset control
        /// </summary>
        private void ResetControl()
        {
            cbbLiLuan.SelectedItem.Value = string.Empty;
            txtGhiChu.Text = string.Empty;
            dtNgayHieuLuc.Text = string.Empty;
        }
        /// <summary>
        /// Show Dialog
        /// </summary>
        /// <param name="type"></param>
        /// <param name="hdId"></param>
        /// <param name="nhanvienID"></param>
        /// <param name="typeParent"></param>
        [DirectMethod]
        public void ShowDialog(string type, string hdId, string nhanvienID, string typeParent)
        {
            storeLiLuan.DataSource = common.LoadDataCommonByCodeKey(common.LyLuanChinhTri);
            storeLiLuan.DataBind();

            hdNhanVienId.Text = nhanvienID;
            hdLiLuanID.Text = hdId;
            if (type.Equals("AddNew"))
            {
                wdLiLuan.Title = "Tạo mới lí luận chính trị";
                ResetControl();
                hdType.Text = type;
                btnSave.Hidden = false;
                hdParentType.Text = typeParent;
            }
            else if (type.Equals("Edit"))
            {
                wdLiLuan.Title = "Sửa đổi lí luận chính trị";
                hdType.Text = type;
                btnSave.Hidden = false;
                LoadData(Convert.ToInt16(hdId));
            }
            else if (type.Equals("View"))
            {
                wdLiLuan.Title = "Xem chi tiết lí luận chính trị";
                hdType.Text = type;
                btnSave.Hidden = true;
                LoadData(Convert.ToInt16(hdId));
            }
            wdLiLuan.Show();
        }
        /// <summary>
        /// Load Data theo id
        /// </summary>
        /// <param name="id"></param>
        public void LoadData(int id)
        {
            lst = (List<NSLiLuanChinhTri>)Session["LiLuan"];
            NSLiLuanChinhTri item = lst.Where(a => a.LiLuanChinhTriID.Equals(id)).SingleOrDefault();
            dtNgayHieuLuc.Text = item.NgayHieuLuc.HasValue && !item.NgayHieuLuc.Value.Year.Equals(1) ? item.NgayHieuLuc.Value.ToString("dd/MM/yyyy") : string.Empty;
            txtGhiChu.Text = item.GhiChu;
            cbbLiLuan.SelectedItem.Value = item.LoaiLiLuanChinhTriID.ToString();
        }
        /// <summary>
        /// ButtonSave
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_CLick(object sender, DirectEventArgs e)
        {
            int nhanVienId = !string.IsNullOrEmpty(hdNhanVienId.Text) ? Convert.ToInt16(hdNhanVienId.Text) : 0;
            int liLuanID = Convert.ToInt16(cbbLiLuan.SelectedItem.Value);
            DateTime ngayHieuLuc = !dtNgayHieuLuc.Text.Equals("01/01/0001") ? Convert.ToDateTime(dtNgayHieuLuc.Text) : DateTime.MinValue;
            if (hdType.Text.Equals("AddNew"))
            {
                NSLiLuanChinhTri item = liLuan.GetItem(nhanVienId, liLuanID, txtGhiChu.Text, ngayHieuLuc);
                ((UCNhanVien)this.Parent).GetLiLuanChinhTri(item);
            }
            else if (hdType.Text.Equals("Edit"))
            {
                liLuan.Edit(Convert.ToInt16(hdLiLuanID.Text), liLuanID, txtGhiChu.Text, ngayHieuLuc);
                ((UCNhanVien)this.Parent).ReloadGrid("LiLuan");
            }

            wdLiLuan.Hide();
        }
    }
}